#!/usr/bin/perl -w
use Net::SMTP;

$msg=<<'END';
From: John Doe <1379205553@qq.com>
To: L Stein <thy_shenzhen@163.com>
Cc: 446789593@qq.com
Subject: hello there


This is just a simple e-mail message.
Nothing to get excited about.
Regards,JD
END

mail_perl($msg,'smtp.163.com') or die "mail_perl error!\n";

sub mail_perl {
        my ($msg,$server)=@_;
        my ($header,$body)=split /\n\n/,$msg,2;
        return warn "no header" unless $header && $body;

        $header=~ s/\n\s+/ /gm;
        my (%fields)=$header=~ /([\w-]+):\s+(.+)$/mg;
        my $from=$fields{From};
        my @to =split /\s*,\s*/,$fields{To};
        push @to,split /\s*,\s*/,$fields{Cc};


        my $smtp = Net::SMTP->new($server) or return warn "couldn't open server";
        $smtp->mail($from) or return warn $smtp->message;
        my @ok = $smtp->recipient(@to,{SkipBad=>1}) or return warn $smtp->message;
        warn $smtp->message unless $ok== @to;
        $smtp->data($msg) or return warn $smtp->message;
        $smtp->quit;
}
